﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Globalization;
using SMS.Entities.Report;
using SMS.Entities.BaseClasses;

namespace SMS.Converter
{
    public class OnlyDateConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value is DateTime)
            {
                return System.Convert.ToDateTime(value).ToShortDateString();
            }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

            //var groupNo = reportData.GetType().GetProperty("GroupNo").GetValue(reportData, null);
            //var year = reportData.GetType().GetProperty("Year").GetValue(reportData, null);
    public class ReportLinkConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var reportData = value as BaseEntity;
            string link = string.Empty;// "/Reports/SalesHistoryReport?option={0}&fromdate={1}&todate={2}";
            string[] arrParameter = System.Convert.ToString(parameter).Split('-');
            switch (System.Convert.ToString(arrParameter[1]))
            {
                case "SaleReport":
                    link = "/Reports/SalesHistoryReport?option={0}&fromdate={1}&todate={2}";
                    break;
                case "ProductSaleReport":
                    link = "/Reports/ProductSaleReport?option={0}&fromdate={1}&todate={2}";
                    break;
                case "CustomerReport":
                    link = "/Reports/CustomerReport?option={0}&fromdate={1}&todate={2}";
                    break;
                case "IndividualCustomerReport":
                    link = "/Reports/IndividualCustomerReport?option={0}&fromdate={1}&todate={2}";
                    break;
                case "NewCustomerReport":
                    link = "/Reports/NewCustomerReport?option={0}&fromdate={1}&todate={2}";
                    break;
                default:
                    break;
            }
            
            string drillDown = string.Empty;
            DateTime fromDate = DateTime.Now;
            DateTime toDate = DateTime.Now;
            switch (System.Convert.ToString(arrParameter[0]))
            {
                case "Weekly":
                    fromDate = Helper.DateTimeHelper.FirstDateOfTheWeek(reportData.GroupNo, reportData.Year);
                    toDate = Helper.DateTimeHelper.LastDateOfTheWeek(reportData.GroupNo, reportData.Year);
                    drillDown = "Daily";
                    break;
                case "Monthly":
                    fromDate = Helper.DateTimeHelper.FirstDateOfTheMonth(reportData.GroupNo, reportData.Year);
                    toDate = Helper.DateTimeHelper.LastDateOfTheMonth(reportData.GroupNo, reportData.Year);
                    drillDown = "Weekly";
                    break;
                case "Quarterly":
                    fromDate = Helper.DateTimeHelper.FirstDateOfQuarter(reportData.GroupNo, reportData.Year);
                    toDate = Helper.DateTimeHelper.LastDateOfQarter(reportData.GroupNo, reportData.Year);
                    drillDown = "Monthly";
                    break;
                case "HalfYearly":
                    fromDate = Helper.DateTimeHelper.FirstDateOfHalfYear(reportData.GroupNo, reportData.Year);
                    toDate = Helper.DateTimeHelper.LastDateOfHalfYear(reportData.GroupNo, reportData.Year);
                    drillDown = "Quarterly";
                    break;
                case "Yearly":
                    fromDate = Helper.DateTimeHelper.FirstDateOfYear(reportData.GroupNo);
                    toDate = Helper.DateTimeHelper.LastDateOfYear(reportData.GroupNo);
                    drillDown = "Half Yearly";
                    break;
                default:
                    break;
            }
            link = string.Format(link, System.Convert.ToString(drillDown), fromDate, toDate);
            return link;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ReportLinkContentConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var reportData = value as BaseEntity;
            string content = string.Empty;
            GregorianCalendar _gc = new GregorianCalendar();
            switch (System.Convert.ToString(parameter))
            {
                case "Yearly":
                    return "Year of "+ reportData.GroupNo;
                case "HalfYearly":
                    return "Half year " + reportData.GroupNo;
                case "Quarterly":
                    return "Quarter " + reportData.GroupNo;
                case "Monthly":
                    return CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[reportData.GroupNo - 1];
                case "Weekly":
                    int totaldays = (reportData.GroupNo-1) * 7;
                    var date = new DateTime(reportData.Year, 1, 1).AddDays(totaldays + 1);                    
                    return "Week " + GetWeekOfMonth(date) + "of " + CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[date.Month - 1];
                case "Daily":
                    return new DateTime(reportData.Year, 1, 1).AddDays(reportData.GroupNo).ToShortDateString();
                default:
                    break;
            }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        private int GetWeekOfMonth(DateTime dateValue)
        {
            if (dateValue.Day % 7 == 0)
                return dateValue.Day / 7;
            return dateValue.Day / 7 + 1;
        }

    }


    public class TrackingIsPaidConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool ispaid = System.Convert.ToBoolean(value);
            if (ispaid)
            {
                return "Yes";
            }
            else
            {
                return "No";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class RetailSaleVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var val = value as Entities.ProductStockWise;
            if (val != null && val.SubQuantity > val.Quantity)
                return Visibility.Visible;
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
